import Vue from "vue";
import AppPrompt from "./app-prompt.vue";
import { Subject } from 'rxjs';

export default class Prompt {

    /**
     * 实例对象
     *
     * @private
     * @static
     * @memberof Prompt
     */
    private static prompt: any;

    /**
     * vue 实例
     *
     * @private
     * @type {Vue}
     * @memberof Prompt
     */
    private vm!: Vue;


    /**
     * 单例构造函数
     * 
     * @memberof Prompt
     */
    private constructor() {
        if (Prompt.prompt) {
            return Prompt.prompt;
        }
    }

    /**
     * 静态获取单例对象方法
     *
     * @static
     * @returns {Prompt}
     * @memberof Prompt
     */
    public static getInstance(): Prompt {
        if (!Prompt.prompt) {
            Prompt.prompt = new Prompt();
        }
        return Prompt.prompt;
    }

    /**
     * 打开弹出框
     *
     * @param {{ mode: string, options: { title?: string, message: string } }} props
     * mode:弹出框模式 title:标题 message:弹出信息
     * @returns {Subject<any>}
     * @memberof Prompt
     */
    public openPrompt(props: { mode: string, options: { title?: string, message: string } }): Subject<any> {
        try {
            let component = AppPrompt;
            const vm = new Vue({
                render(h) {
                    return h(component, { props });
                }
            }).$mount();
            this.vm = vm;
            document.body.appendChild(vm.$el);
            const comp: any = vm.$children[0];
            return comp.show();
        } catch (error) {
            console.log(error);
            return new Subject<any>();
        }
    }

}